﻿using System;
using System.Windows;
using System.Windows.Navigation;

using Microsoft.Phone.Controls;
using PhoneGestureEventArgs = Microsoft.Phone.Controls.GestureEventArgs;

using SSTU.Extensions;
using SSTU.ViewModel;
using SSTU.ViewModel.Item;
using System.Windows.Controls;

namespace SSTU.View
{
	public partial class FacultyMenuPage : PhoneApplicationPage
	{
		public const string NavigationParameterNameFacultyId = "FacultyId";
		public const string NavigationParameterNameFacultyShortName = "FacultyShortName";
		public const string NavigationParameterNameFacultyLongName = "FacultyLongName";

		public FacultyMenuPage()
		{
			InitializeComponent();
		}

		public FacultyMenuViewModel ViewModel
		{
			get { return DataContext as FacultyMenuViewModel; }
			set { DataContext = value; }
		}

		protected override void OnNavigatedTo(NavigationEventArgs e)
		{
			if (ViewModel != null)
			{
				string departmentId = NavigationContext.QueryString.Get(NavigationParameterNameFacultyId);
				string departmentShortName = NavigationContext.QueryString.Get(NavigationParameterNameFacultyShortName);
				string departmentLongName = NavigationContext.QueryString.Get(NavigationParameterNameFacultyLongName);
				var departmentItem = new FacultyItemVM(departmentId, departmentShortName, departmentLongName);
				ViewModel.Initialize(departmentItem);
			}
		}

		private void OnGroupItemTap(object sender, PhoneGestureEventArgs e)
		{
			var originalSource = e.OriginalSource as DependencyObject;
			var contextMenu = ContextMenuService.GetContextMenu(originalSource);
			if ((ViewModel != null) && (contextMenu != null) && !contextMenu.IsOpen)
			{
				var groupItem = ((FrameworkElement)sender).DataContext as GroupItemVM;
				if (groupItem != null)
				{
					ViewModel.OpenGroup(groupItem);
				}
			}
		}
	}
}